package com.hgxd.log;

import com.hgxd.annotation.OperationLogAnnotation;
import com.hgxd.entity.pojo.OperateLog;
import com.hgxd.mapper.OperateLogMapper;
import com.hgxd.utils.security.JwtHelper;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;

/**
 * @author wtf
 * @create 2023-10-23 10:08
 * @description :
 */
@Aspect
@Component
public class OperationLogAspect {
    @Autowired
    OperateLogMapper operateLogMapper;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    /**
     * 设置操作日志切入点   在注解的位置切入代码
     */
    @Pointcut("@annotation(com.hgxd.annotation.OperationLogAnnotation)")
    public void operLogPoinCut() {
    }


    @AfterReturning(returning  /**
     * 记录操作日志
     * @param joinPoint 方法的执行点
     * @param result  方法返回值
     * @throws Throwable
     */ = "result", value = "operLogPoinCut()")
    public void saveOperLog(JoinPoint joinPoint, Object result) throws Throwable {
        // 获取RequestAttributes
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        // 从获取RequestAttributes中获取HttpServletRequest的信息
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        try {
            //将返回值转换成map集合
//            Result r = (Result)result;
//            System.out.println("r = " + r);
            OperateLog operateLog = new OperateLog();
            // 从切面织入点处通过反射机制获取织入点处的方法
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            //获取切入点所在的方法
            Method method = signature.getMethod();
            //获取操作
            OperationLogAnnotation annotation = method.getAnnotation(OperationLogAnnotation.class);
            if (annotation != null) {
                operateLog.setModule(annotation.operModule());
                operateLog.setType(annotation.operType());
                operateLog.setComment(annotation.operCommont());
                operateLog.setContent(annotation.operContent());
            }
            //操作时间
//            operateLog.setOperationTime(Timestamp.valueOf(sdf.format(new Date())));
            operateLog.setDate(LocalDateTime.now());
            //操作用户

            operateLog.setUserName(JwtHelper.getUserName(request.getHeader("token")));
            //操作IP
            String ip = request.getRemoteAddr();
            operateLog.setIp(ip);
            //返回值信息
//            operateLog.setResult(map.get("message"));
            //保存日志
//            operateLogMapper.save(operateLog);
            operateLogMapper.insert(operateLog);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
